export MAKE_ROOT=$(shell pwd)


NULLSTRING:=
SPACE:=$(NULLSTRING) # end of the line


include $(MAKE_ROOT)/env.mk
#-include $(wildcard $(addsuffix /*.d,$(DEP_PATHS)))

# iterate each sub dir and execute it's makefile
define build_obj
for SubDir in $(SRC_PATHS); do \
    if ! [ -d $$SubDir ]; then \
        echo "The $$SubDir is not exist !"; \
        exit 11; \
    fi; \
    echo "Building $$SubDir ..."; \
    make -C $$SubDir ; \
    if [ $$? -ne 0 ]; then \
        echo "Building $$SubDir is failed !"; \
        exit 12; \
    fi; \
done
endef


all:init
	@$(call build_obj)
ifeq ($(LIB_TYPE_STATIC),$(LIB_TYPE))
	$(AR) $(ARFLAGS) $(LIB_ROOT)/$(TARGET) $(addsuffix /*.o,$(OBJ_PATHS))
else
	$(CC) $(LDFLAGS) -o $(LIB_ROOT)/$(TARGET) $(addsuffix /*.o,$(OBJ_PATHS))
endif


# create all dirs
init:  
	mkdir -p $(OBJ_ROOT)
	mkdir -p $(DEP_ROOT)
	mkdir -p $(LIB_ROOT)
#	mkdir -p $(OBJ_PATHS)

clean:
	-rm -rf $(OBJ_ROOT)
	-rm -rf $(DEP_ROOT)
	-rm -rf $(LIB_ROOT)
	-rm -rf $(OUT_ROOT)